﻿
    using Chain.BLL;
    using System;
    using System.Data;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class historyexpenseByStoresDetail : Page
    {
        private Goods GoodsBll = new Goods();
        private OrderDetail OrderDetailBll = new OrderDetail();
        private string OrderType = string.Empty;

        private decimal TotalSubtotal;

        private void BindGoodsinventory(int OrderID)
        {
            DataTable dt = this.OrderDetailBll.GetList("OrderID=" + OrderID).Tables[0];
            StringBuilder sbWhere = new StringBuilder("GoodsID in(");
            StringBuilder sbKeys = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbWhere.AppendFormat("{0},", dt.Rows[i]["GoodsID"]);
                sbKeys.AppendFormat("{0},{1}|", dt.Rows[i]["GoodsID"], dt.Rows[i]["OrderDetailNumber"]);
            }
            sbWhere.Append("0)");
            DataTable dtGoods = this.GoodsBll.GetList(sbWhere.ToString()).Tables[0];
            string[] keys = sbKeys.ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            dtGoods = this.UpDateTable(dtGoods, keys);
            this.rptGoodsinventory.DataSource = dtGoods;
            this.rptGoodsinventory.DataBind();
            this.total.InnerText = (this.TotalSubtotal < 0M) ? "0" : this.TotalSubtotal.ToString("0.00");
        }

        protected string GetTitleName()
        {
            if (this.OrderType == "7")
            {
                return "计次数量";
            }
            return "购买数量";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((base.Request.Cookies["uid"] != null) && Regex.IsMatch(base.Request.Cookies["uid"].Value, @"^\d+$"))
            {
                int OrderID = Convert.ToInt32(base.Request["OrderID"]);
                Convert.ToInt32(base.Request.Cookies["uid"].Value);
                this.OrderType = base.Request["OrderType"];
                this.BindGoodsinventory(OrderID);
            }
        }

        private DataTable UpDateTable(DataTable dt, string[] keys)
        {
            dt.Columns.Add("ConvertNumber");
            dt.Columns.Add("Subtotal");
            foreach (string item in keys)
            {
                int MicroGoodsID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                DataRow row = dt.Select("GoodsID=" + MicroGoodsID)[0];
                row["ConvertNumber"] = ConvertNumber;
                row["Subtotal"] = ConvertNumber * Convert.ToInt32(row["Price"]);
                this.TotalSubtotal += Convert.ToInt32(row["Subtotal"]);
            }
            return dt;
        }
    }


